<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Java Stream中的API你都用过了吗？ | 架构成长指南</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
    <meta name="description" content="java技术分享，系统架构剖析，java实践分享，ai相关知识分享，chatGPT,架构成长指南">
  
  
    <meta name="keywords" content="架构成长指南，架构，java，云原生，chatGPT">
  
  
    <link rel="alternate" href="/atom.xml" title="架构成长指南" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="/localshare/css/share.css">

  
  
    
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">

  
  
<meta name="generator" content="Hexo 7.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">架构成长指南</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/."><i class="fa fa-home"></i> 首页</a>
        
          <a class="main-nav-link" href="/archives/"><i class="fa fa-archive"></i> 归档</a>
        
          <a class="main-nav-link" href="/about/"><i class="fa fa-user"></i> 关于</a>
        
      </nav>
    </div>
    <div id="search-form">
      <div id="result-mask" class="hide"></div>
      <label><input id="search-key" type="text" autocomplete="off" placeholder="搜索"></label>
      <div id="result-wrap" class="hide">
        <div id="search-result"></div>
      </div>
      <div class="hide">
        <template id="search-tpl">
          <div class="item">
            <a href="/{path}" title="{title}">
              <div class="title">{title}</div>
              <div class="time">{date}</div>
              <div class="tags">{tags}</div>
            </a>
          </div>
        </template>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-2023/java/JavaStream" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Java Stream中的API你都用过了吗？
    </h1>
  


      </header>
    
    <div class="article-meta">
      
      <span class="article-date">
  <i class="fa fa-date"></i>
  <time class="dt-published" datetime="2023-11-21T01:22:41.327Z" itemprop="datePublished">2023年11月21日</time>
</span>
      
  <div class="article-category">
    <i class="fa fa-classify"></i>
    <a class="article-category-link" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a>
  </div>

      
        <span class="article-views">
  <i class="fa fa-views"></i>
  <i id="busuanzi_container_page_pv">
      <i id="busuanzi_value_page_pv"></i>
  </i>
</span>

      
      
<a href="/2023/11/21/2023/java/JavaStream/#comments" class="article-comment-link">
  
    
    
    
    
    
  
  <i class="fa fa-commt"></i>
  留言
</a>


    </div>
    <div class="e-content article-entry" itemprop="articleBody">
      
        <blockquote>
<p>公众号「架构成长指南」，专注于生产实践、云原生、分布式系统、大数据技术分享。</p>
</blockquote>
<p>在本教程中，您将通过大量示例来学习 Java 8 Stream API。</p>
<p>Java 在 Java 8 中提供了一个新的附加包，称为 <code>java.util.stream</code>。该包由类、接口和枚举组成，允许对元素进行函数式操作。 您可以通过在程序中导入 <code>java.util.stream</code>包来使用流。</p>
<h3 id="Stream提供以下功能："><a href="#Stream提供以下功能：" class="headerlink" title="Stream提供以下功能："></a>Stream提供以下功能：</h3><p>Stream不存储元素。它只是通过计算操作的管道传送来自数据结构、数组或 I&#x2F;O 通道等源的元素。</p>
<p>Stream本质上是函数式的，对流执行的操作不会修改其源。例如，过滤从集合获取的 Stream 会生成一个没有过滤元素的新 Stream，而不是从源集合中删除元素。</p>
<p>Stream是惰性的，仅在需要时才计算代码，在流的生命周期中，流的元素仅被访问一次。</p>
<p>与迭代器一样，必须生成新流才能重新访问源中的相同元素。<br>您可以使用 Stream 来 过滤、收集、打印以及 从一种数据结构转换为其他数据结构等。</p>
<span id="more"></span>
<p><img src="https://files.mdnice.com/user/35072/63a23482-e1ac-43c5-a933-70e845a0505a.png"></p>
<h3 id="Stream-API-示例"><a href="#Stream-API-示例" class="headerlink" title="Stream API 示例"></a>Stream API 示例</h3><h4 id="1-创建一个空的Stream"><a href="#1-创建一个空的Stream" class="headerlink" title="1. 创建一个空的Stream"></a>1. 创建一个空的Stream</h4><p>在创建空流时，应使用 empty() 方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Stream&lt;String&gt; stream = Stream.empty();</span><br><span class="line">stream.forEach(System.out::println);</span><br></pre></td></tr></table></figure>
<p>通常情况下，在创建时会使用 <code>empty()</code> 方法，以避免在没有元素的流中返回 null：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> Stream&lt;String&gt; <span class="title function_">streamOf</span><span class="params">(List&lt;String&gt; list)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> list == <span class="literal">null</span> || list.isEmpty() ? Stream.empty() : list.stream();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-从集合中创建流"><a href="#2-从集合中创建流" class="headerlink" title="2.从集合中创建流"></a>2.从集合中创建流</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.Collection;</span><br><span class="line"><span class="keyword">import</span> java.util.HashSet;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Stream;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamCreationExamples</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"></span><br><span class="line">        Collection&lt;String&gt; collection = Arrays.asList(<span class="string">&quot;JAVA&quot;</span>, <span class="string">&quot;J2EE&quot;</span>, <span class="string">&quot;Spring&quot;</span>, <span class="string">&quot;Hibernate&quot;</span>);</span><br><span class="line">        Stream&lt;String&gt; stream2 = collection.stream();</span><br><span class="line">        stream2.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">        List&lt;String&gt; list = Arrays.asList(<span class="string">&quot;JAVA&quot;</span>, <span class="string">&quot;J2EE&quot;</span>, <span class="string">&quot;Spring&quot;</span>, <span class="string">&quot;Hibernate&quot;</span>);</span><br><span class="line">        Stream&lt;String&gt; stream3 = list.stream();</span><br><span class="line">        stream3.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">        Set&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;&gt;(list);</span><br><span class="line">        Stream&lt;String&gt; stream4 = set.stream();</span><br><span class="line">        stream4.forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>输出</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">JAVA</span><br><span class="line">J2EE</span><br><span class="line">Spring</span><br><span class="line">Hibernate</span><br><span class="line">JAVA</span><br><span class="line">J2EE</span><br><span class="line">Spring</span><br><span class="line">Hibernate</span><br><span class="line">JAVA</span><br><span class="line">Hibernate</span><br><span class="line">J2EE</span><br><span class="line">Spring</span><br></pre></td></tr></table></figure>
<h4 id="3-从数组中创建流对象"><a href="#3-从数组中创建流对象" class="headerlink" title="3. 从数组中创建流对象"></a>3. 从数组中创建流对象</h4><p>数组可以是流的源，也可以从现有数组或数组的一部分创建数组：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Stream;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamCreationExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 使用Arrays.stream()创建流</span></span><br><span class="line">        <span class="type">int</span>[] numbers = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">        Stream&lt;Integer&gt; stream1 = Arrays.stream(numbers);</span><br><span class="line">        System.out.println(<span class="string">&quot;Using Arrays.stream():&quot;</span>);</span><br><span class="line">        stream1.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用Stream.of()创建流</span></span><br><span class="line">        String[] names = &#123;<span class="string">&quot;Alice&quot;</span>, <span class="string">&quot;Bob&quot;</span>, <span class="string">&quot;Charlie&quot;</span>&#125;;</span><br><span class="line">        Stream&lt;String&gt; stream2 = Stream.of(names);</span><br><span class="line">        System.out.println(<span class="string">&quot;Using Stream.of():&quot;</span>);</span><br><span class="line">        stream2.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用Stream.builder()创建流</span></span><br><span class="line">        String[] colors = &#123;<span class="string">&quot;Red&quot;</span>, <span class="string">&quot;Green&quot;</span>, <span class="string">&quot;Blue&quot;</span>&#125;;</span><br><span class="line">        Stream.Builder&lt;String&gt; builder = Stream.builder();</span><br><span class="line">        <span class="keyword">for</span> (String color : colors) &#123;</span><br><span class="line">            builder.add(color);</span><br><span class="line">        &#125;</span><br><span class="line">        Stream&lt;String&gt; stream3 = builder.build();</span><br><span class="line">        System.out.println(<span class="string">&quot;Using Stream.builder():&quot;</span>);</span><br><span class="line">        stream3.forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Using Arrays.stream():</span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">Using Stream.of():</span><br><span class="line">Alice</span><br><span class="line">Bob</span><br><span class="line">Charlie</span><br><span class="line">Using Stream.builder():</span><br><span class="line">Red</span><br><span class="line">Green</span><br><span class="line">Blue</span><br></pre></td></tr></table></figure>
<h4 id="4-使用Stream过滤一个集合示例"><a href="#4-使用Stream过滤一个集合示例" class="headerlink" title="4. 使用Stream过滤一个集合示例"></a>4. 使用Stream过滤一个集合示例</h4><p>在下面的示例中，我们不使用流过滤数据，看看代码是什么样的，同时我们在给出一个使用stream过滤的示例，对比一下</p>
<p><strong>不使用Stream过滤一个集合示例</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FilterWithoutStreamExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Integer&gt; numbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        numbers.add(<span class="number">10</span>);</span><br><span class="line">        numbers.add(<span class="number">20</span>);</span><br><span class="line">        numbers.add(<span class="number">30</span>);</span><br><span class="line">        numbers.add(<span class="number">40</span>);</span><br><span class="line">        numbers.add(<span class="number">50</span>);</span><br><span class="line"></span><br><span class="line">        List&lt;Integer&gt; filteredNumbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (Integer number : numbers) &#123;</span><br><span class="line">            <span class="keyword">if</span> (number &gt; <span class="number">30</span>) &#123;</span><br><span class="line">                filteredNumbers.add(number);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;Filtered numbers (without Stream):&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (Integer number : filteredNumbers) &#123;</span><br><span class="line">            System.out.println(number);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Filtered <span class="title function_">numbers</span> <span class="params">(without Stream)</span>:</span><br><span class="line"><span class="number">40</span></span><br><span class="line"><span class="number">50</span></span><br></pre></td></tr></table></figure>
<p><strong>使用 Stream 过滤集合示例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FilterWithStreamExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Integer&gt; numbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        numbers.add(<span class="number">10</span>);</span><br><span class="line">        numbers.add(<span class="number">20</span>);</span><br><span class="line">        numbers.add(<span class="number">30</span>);</span><br><span class="line">        numbers.add(<span class="number">40</span>);</span><br><span class="line">        numbers.add(<span class="number">50</span>);</span><br><span class="line"></span><br><span class="line">        List&lt;Integer&gt; filteredNumbers = numbers.stream()</span><br><span class="line">                .filter(number -&gt; number &gt; <span class="number">30</span>)</span><br><span class="line">                .toList();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;Filtered numbers (with Stream):&quot;</span>);</span><br><span class="line">        filteredNumbers.forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Filtered <span class="title function_">numbers</span> <span class="params">(with Stream)</span>:</span><br><span class="line"><span class="number">40</span></span><br><span class="line"><span class="number">50</span></span><br></pre></td></tr></table></figure>
<p>前后我们对比一下，可以看到，使用 Stream 进行集合过滤可以更加简洁和直观，减少了手动迭代和添加元素的步骤。它提供了一种声明式的编程风格，使代码更易读、可维护和可扩展。</p>
<h4 id="5-使用Stream过滤和遍历集合"><a href="#5-使用Stream过滤和遍历集合" class="headerlink" title="5. 使用Stream过滤和遍历集合"></a>5. 使用Stream过滤和遍历集合</h4><p>在下面的示例中，我们使用 filter() 方法进行过滤，使用 forEach() 方法对数据流进行迭代：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FilterAndIterateWithStreamExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;String&gt; names = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        names.add(<span class="string">&quot;Alice&quot;</span>);</span><br><span class="line">        names.add(<span class="string">&quot;Bob&quot;</span>);</span><br><span class="line">        names.add(<span class="string">&quot;Charlie&quot;</span>);</span><br><span class="line">        names.add(<span class="string">&quot;David&quot;</span>);</span><br><span class="line">        names.add(<span class="string">&quot;Eve&quot;</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;Filtered names starting with &#x27;A&#x27;:&quot;</span>);</span><br><span class="line">        names.stream()</span><br><span class="line">                .filter(name -&gt; name.startsWith(<span class="string">&quot;A&quot;</span>))</span><br><span class="line">                .forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Filtered names starting with <span class="string">&#x27;A&#x27;</span>:</span><br><span class="line">Alice</span><br></pre></td></tr></table></figure>
<p>在上述示例中，我们有一个字符串列表 <code>names</code>，其中包含了一些名字。<br>我们使用 Stream 进行过滤和迭代操作以查找以字母 “A” 开头的名字。</p>
<h4 id="6-使用Collectors方法求和"><a href="#6-使用Collectors方法求和" class="headerlink" title="6.使用Collectors方法求和"></a>6.使用<code>Collectors</code>方法求和</h4><p>我们还可以使用<code>Collectors</code>计算数值之和。</p>
<p>在下面的示例中，我们使用<code>Collectors</code>类及其指定方法计算所有产品价格的总和。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SumByUsingCollectorsMethods</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List &lt; Product &gt; productsList = <span class="keyword">new</span> <span class="title class_">ArrayList</span> &lt; Product &gt; ();</span><br><span class="line">        productsList.add(<span class="keyword">new</span> <span class="title class_">Product</span>(<span class="number">1</span>, <span class="string">&quot;HP Laptop&quot;</span>, <span class="number">25000f</span>));</span><br><span class="line">        productsList.add(<span class="keyword">new</span> <span class="title class_">Product</span>(<span class="number">2</span>, <span class="string">&quot;Dell Laptop&quot;</span>, <span class="number">30000f</span>));</span><br><span class="line">        productsList.add(<span class="keyword">new</span> <span class="title class_">Product</span>(<span class="number">3</span>, <span class="string">&quot;Lenevo Laptop&quot;</span>, <span class="number">28000f</span>));</span><br><span class="line">        productsList.add(<span class="keyword">new</span> <span class="title class_">Product</span>(<span class="number">4</span>, <span class="string">&quot;Sony Laptop&quot;</span>, <span class="number">28000f</span>));</span><br><span class="line">        productsList.add(<span class="keyword">new</span> <span class="title class_">Product</span>(<span class="number">5</span>, <span class="string">&quot;Apple Laptop&quot;</span>, <span class="number">90000f</span>));</span><br><span class="line">        </span><br><span class="line">        <span class="type">double</span> <span class="variable">totalPrice3</span> <span class="operator">=</span> productsList.stream()</span><br><span class="line">            .collect(Collectors.summingDouble(product -&gt; product.getPrice()));</span><br><span class="line">        System.out.println(totalPrice3);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">201000.0</span><br></pre></td></tr></table></figure>
<h4 id="7-使用Stream查找年龄最大和最小的学生"><a href="#7-使用Stream查找年龄最大和最小的学生" class="headerlink" title="7. 使用Stream查找年龄最大和最小的学生"></a>7. 使用Stream查找年龄最大和最小的学生</h4><p>假设有一个 Student 类具有 name 和 age 属性。我们可以使用 Stream 来查找学生集合中年龄的最大和最小值，并打印出相应的学生信息。以下是一个示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentStreamExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Student&gt; students = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Alice&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Bob&quot;</span>, <span class="number">22</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Charlie&quot;</span>, <span class="number">19</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;David&quot;</span>, <span class="number">21</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找年龄最大的学生</span></span><br><span class="line">        Optional&lt;Student&gt; maxAgeStudent = students.stream()</span><br><span class="line">                .max(Comparator.comparingInt(Student::getAge));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找年龄最小的学生</span></span><br><span class="line">        Optional&lt;Student&gt; minAgeStudent = students.stream()</span><br><span class="line">                .min(Comparator.comparingInt(Student::getAge));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印最大和最小年龄的学生信息</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Student with maximum age:&quot;</span>);</span><br><span class="line">        maxAgeStudent.ifPresent(System.out::println);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;Student with minimum age:&quot;</span>);</span><br><span class="line">        minAgeStudent.ifPresent(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Student with maximum age:</span><br><span class="line">Student&#123;name=&#x27;Bob&#x27;, age=22&#125;</span><br><span class="line">Student with minimum age:</span><br><span class="line">Student&#123;name=&#x27;Charlie&#x27;, age=19&#125;</span><br></pre></td></tr></table></figure>
<h4 id="8-使用Stream转换List为Map"><a href="#8-使用Stream转换List为Map" class="headerlink" title="8. 使用Stream转换List为Map"></a>8. 使用Stream转换List为Map</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentStreamToMapExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Student&gt; students = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Alice&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Bob&quot;</span>, <span class="number">22</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Charlie&quot;</span>, <span class="number">19</span>));</span><br><span class="line">        students.add(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;David&quot;</span>, <span class="number">21</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将学生列表转换为 Map，以姓名为键，学生对象为值</span></span><br><span class="line">        Map&lt;String, Student&gt; studentMap = students.stream()</span><br><span class="line">                .collect(Collectors.toMap(Student::getName, student -&gt; student));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印学生 Map</span></span><br><span class="line">        <span class="keyword">for</span> (Map.Entry&lt;String, Student&gt; entry : studentMap.entrySet()) &#123;</span><br><span class="line">            System.out.println(entry.getKey() + <span class="string">&quot;: &quot;</span> + entry.getValue());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">David: Student&#123;name=&#x27;David&#x27;, age=21&#125;</span><br><span class="line">Bob: Student&#123;name=&#x27;Bob&#x27;, age=22&#125;</span><br><span class="line">Charlie: Student&#123;name=&#x27;Charlie&#x27;, age=19&#125;</span><br><span class="line">Alice: Student&#123;name=&#x27;Alice&#x27;, age=20&#125;</span><br></pre></td></tr></table></figure>
<p>在上面示例中，我们使用<code>Collectors.toMap()</code> 方法将学生列表转换为 Map。我们指定了键提取器 <code>Student::getName</code>，将学生的姓名作为键。对于值提取器，我们使用了一个匿名函数 <code>student -&gt; student</code>，它返回学生对象本身作为值。</p>
<h4 id="9-使用Stream把List对象转换为另一个List对象"><a href="#9-使用Stream把List对象转换为另一个List对象" class="headerlink" title="9. 使用Stream把List对象转换为另一个List对象"></a>9. 使用Stream把List对象转换为另一个List对象</h4><p>假设我们有一个 Person 类，其中包含姓名和年龄属性。我们可以使用 Stream 来将一个 List<Person> 对象转换为另一个 List<String> 对象，其中只包含人员的姓名。以下是一个示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ListTransformationExample</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Person&gt; persons = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        persons.add(<span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Alice&quot;</span>, <span class="number">20</span>));</span><br><span class="line">        persons.add(<span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Bob&quot;</span>, <span class="number">22</span>));</span><br><span class="line">        persons.add(<span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Charlie&quot;</span>, <span class="number">19</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将 Person 列表转换为只包含姓名的 String 列表</span></span><br><span class="line">        List&lt;String&gt; names = persons.stream()</span><br><span class="line">                .map(Person::getName)</span><br><span class="line">                .collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印转换后的姓名列表</span></span><br><span class="line">        System.out.println(names);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">```  </span><br><span class="line">输出：</span><br></pre></td></tr></table></figure>
<p>  [Alice, Bob, Charlie]</p>
<pre><code>在上述示例中，我们有一个 `Person` 类，其中包含姓名和年龄属性。我们创建了一个 persons 列表，并添加了几个 Person 对象。

使用`Stream`，我们通过调用 `map()` 方法并传入一个方法引用 Person::getName，最后，我们使用 `collect() `方法和 `Collectors.toList()` 将转换后的姓名收集到一个新的列表中。

### API
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
  
</code></pre>

        
            <div id="toc-article">
                
  <div class="widget-wrap" id="toc-wrap">
    <h3 class="widget-title"><i class="fa fa-toc"></i> 文章目录</h3>
    <div class="widget">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Stream%E6%8F%90%E4%BE%9B%E4%BB%A5%E4%B8%8B%E5%8A%9F%E8%83%BD%EF%BC%9A"><span class="toc-text">Stream提供以下功能：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Stream-API-%E7%A4%BA%E4%BE%8B"><span class="toc-text">Stream API 示例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E7%A9%BA%E7%9A%84Stream"><span class="toc-text">1. 创建一个空的Stream</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E4%BB%8E%E9%9B%86%E5%90%88%E4%B8%AD%E5%88%9B%E5%BB%BA%E6%B5%81"><span class="toc-text">2.从集合中创建流</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E4%BB%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E5%88%9B%E5%BB%BA%E6%B5%81%E5%AF%B9%E8%B1%A1"><span class="toc-text">3. 从数组中创建流对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8Stream%E8%BF%87%E6%BB%A4%E4%B8%80%E4%B8%AA%E9%9B%86%E5%90%88%E7%A4%BA%E4%BE%8B"><span class="toc-text">4. 使用Stream过滤一个集合示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E4%BD%BF%E7%94%A8Stream%E8%BF%87%E6%BB%A4%E5%92%8C%E9%81%8D%E5%8E%86%E9%9B%86%E5%90%88"><span class="toc-text">5. 使用Stream过滤和遍历集合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-%E4%BD%BF%E7%94%A8Collectors%E6%96%B9%E6%B3%95%E6%B1%82%E5%92%8C"><span class="toc-text">6.使用Collectors方法求和</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-%E4%BD%BF%E7%94%A8Stream%E6%9F%A5%E6%89%BE%E5%B9%B4%E9%BE%84%E6%9C%80%E5%A4%A7%E5%92%8C%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%A6%E7%94%9F"><span class="toc-text">7. 使用Stream查找年龄最大和最小的学生</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-%E4%BD%BF%E7%94%A8Stream%E8%BD%AC%E6%8D%A2List%E4%B8%BAMap"><span class="toc-text">8. 使用Stream转换List为Map</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-%E4%BD%BF%E7%94%A8Stream%E6%8A%8AList%E5%AF%B9%E8%B1%A1%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%8F%A6%E4%B8%80%E4%B8%AAList%E5%AF%B9%E8%B1%A1"><span class="toc-text">9. 使用Stream把List对象转换为另一个List对象</span></a></li></ol></li></ol>
    </div>
  </div>


            </div>
        
        
          <blockquote id="copyright">
              <p>原文链接: <a href="https://dongweizhao.github.io/2023/11/21/2023/java/JavaStream/">https://dongweizhao.github.io/2023/11/21/2023/java/JavaStream/</a></p>
              <p>版权声明: 转载请注明出处.</p>
          </blockquote>
        
      
    </div>
    <footer class="article-footer">
      
        <div class="article-tag-wrap">
          

          
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Stream/" rel="tag">Stream</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%90%8E%E7%AB%AF/" rel="tag">后端</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%9E%B6%E6%9E%84%E6%88%90%E9%95%BF%E6%8C%87%E5%8D%97/" rel="tag">架构成长指南</a></li></ul>

          
    <div class="social-share">
      <span>分享到:</span>
    </div>



        </div>
      
      
        
<nav id="article-nav">
  
    <a href="/2020/06/29/2022/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E9%97%B4%E4%BB%B6/%E6%A1%88%E4%BE%8B%E5%88%86%E4%BA%AB/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">older</strong>
      <div class="article-nav-title">
        
          04 分库分表的案例分享
        
      </div>
    </a>
  
  
    <a href="/2023/11/23/2023/%E5%B7%A5%E5%85%B7/poe%E5%88%86%E4%BA%AB/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">newer</strong>
      <div class="article-nav-title">
        
          免费稳定几乎无门槛，我的ChartGPT助手免费分享给你
        
      </div>
    </a>
  
</nav>

      
      
        








      
    </footer>
  </div>
</article>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-posts"></i> 最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/12/17/2023/%E4%BA%91%E5%8E%9F%E7%94%9F/%E5%9F%BA%E4%BA%8EK8S%E7%A7%81%E6%9C%89%E5%8C%96%E4%BA%A4%E4%BB%98%E8%A6%81%E6%B3%A8%E6%84%8F%E5%87%A0%E7%82%B9%E9%97%AE%E9%A2%98/">基于K8S私有化交付要注意几点问题</a>
          </li>
        
          <li>
            <a href="/2023/12/17/2023/%E4%BA%91%E5%8E%9F%E7%94%9F/%E4%BB%8E%E7%89%A9%E7%90%86%E6%9C%BA%E5%88%B0K8S%EF%BC%9A%E5%BA%94%E7%94%A8%E7%B3%BB%E7%BB%9F%E9%83%A8%E7%BD%B2%E6%96%B9%E5%BC%8F%E7%9A%84%E6%BC%94%E8%BF%9B%E5%8F%8A%E5%85%B6%E5%BD%B1%E5%93%8D/">从物理机到K8S：应用系统部署方式的演进及其影响</a>
          </li>
        
          <li>
            <a href="/2023/12/17/2023/%E4%BA%91%E5%8E%9F%E7%94%9F/%E4%BB%80%E4%B9%88%E6%98%AF%E9%9D%A9%E5%91%BD%E6%80%A7%E6%8A%80%E6%9C%AFeBPF%EF%BC%9F%E4%B8%BA%E4%BB%80%E4%B9%88%E5%8F%AF%E8%A7%82%E6%B5%8B%E6%80%A7%E9%A2%86%E5%9F%9F%E9%83%BD%E5%BE%97%E7%94%A8%E5%AE%83/">什么是革命性技术eBPF？为什么可观测性领域都得用它</a>
          </li>
        
          <li>
            <a href="/2023/12/17/2023/%E4%BA%91%E5%8E%9F%E7%94%9F/%E5%A6%82%E4%BD%95%E5%9F%BA%E4%BA%8E%20k8s%E5%81%9A%E7%A7%81%E6%9C%89%E5%8C%96%E9%83%A8%E7%BD%B2/">如何基于 k8s做私有化部署</a>
          </li>
        
          <li>
            <a href="/2023/12/17/2023/%E4%BA%91%E5%8E%9F%E7%94%9F/10%E4%B8%AA%E5%B8%B8%E8%A7%81%E7%9A%84%20Kubernetes%20%E9%99%B7%E9%98%B1%E5%92%8C%E6%8C%91%E6%88%98/">10个常见的 Kubernetes 陷阱和挑战</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/AI/" style="font-size: 10px;">AI</a> <a href="/tags/AIGC/" style="font-size: 10px;">AIGC</a> <a href="/tags/Kubernetes/" style="font-size: 17.5px;">Kubernetes</a> <a href="/tags/Stream/" style="font-size: 10px;">Stream</a> <a href="/tags/chatGPT/" style="font-size: 10px;">chatGPT</a> <a href="/tags/eBPF/" style="font-size: 10px;">eBPF</a> <a href="/tags/java/" style="font-size: 15px;">java</a> <a href="/tags/k8s/" style="font-size: 12.5px;">k8s</a> <a href="/tags/k8s10-%E4%B8%AA%E5%B8%B8%E8%A7%81%E6%8C%91%E6%88%98%E5%92%8C%E9%99%B7%E9%98%B1/" style="font-size: 10px;">k8s10 个常见挑战和陷阱</a> <a href="/tags/nio/" style="font-size: 10px;">nio</a> <a href="/tags/%E4%BB%80%E4%B9%88%E6%98%AFeBPF/" style="font-size: 10px;">什么是eBPF</a> <a href="/tags/%E4%BB%8Edocker%E5%88%B0k8s/" style="font-size: 10px;">从docker到k8s</a> <a href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" style="font-size: 17.5px;">分布式</a> <a href="/tags/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8%E5%8F%8A%E4%BA%8B%E5%8A%A1%E4%B8%80%E8%87%B4%E6%80%A7%E8%AE%BE%E8%AE%A1/" style="font-size: 17.5px;">分库分表及事务一致性设计</a> <a href="/tags/%E5%8F%AF%E8%A7%82%E6%B5%8B%E9%A2%86%E5%9F%9F%E5%BA%95%E5%B1%82%E6%8A%80%E6%9C%AF/" style="font-size: 10px;">可观测领域底层技术</a> <a href="/tags/%E5%90%8E%E7%AB%AF/" style="font-size: 10px;">后端</a> <a href="/tags/%E5%A6%82%E4%BD%95%E5%9F%BA%E4%BA%8E-k8s%E5%81%9A%E7%A7%81%E6%9C%89%E5%8C%96%E9%83%A8%E7%BD%B2/" style="font-size: 10px;">如何基于 k8s做私有化部署</a> <a href="/tags/%E5%B9%B6%E5%8F%91/" style="font-size: 15px;">并发</a> <a href="/tags/%E6%9E%B6%E6%9E%84%E6%88%90%E9%95%BF%E6%8C%87%E5%8D%97/" style="font-size: 20px;">架构成长指南</a> <a href="/tags/%E7%A7%81%E6%9C%89%E5%8C%96%E4%BA%A4%E4%BB%98/" style="font-size: 10px;">私有化交付</a> <a href="/tags/%E7%A7%81%E6%9C%89%E5%8C%96%E9%83%A8%E7%BD%B2/" style="font-size: 12.5px;">私有化部署</a> <a href="/tags/%E7%B3%BB%E7%BB%9F%E9%83%A8%E7%BD%B2%E6%9E%B6%E6%9E%84%E6%BC%94%E8%BF%9B/" style="font-size: 10px;">系统部署架构演进</a> <a href="/tags/%E9%93%BE%E8%A1%A8/" style="font-size: 12.5px;">链表</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-classify"></i> 分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E4%BA%91%E5%8E%9F%E7%94%9F/">云原生</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/AI/">AI</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-archive"></i>公众号</h3>
    <div class="widget">
       <img src="/gzh.jpg" style="width:80%; height:80%;" />
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AIGC/" rel="tag">AIGC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Kubernetes/" rel="tag">Kubernetes</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Stream/" rel="tag">Stream</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/chatGPT/" rel="tag">chatGPT</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/eBPF/" rel="tag">eBPF</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/" rel="tag">java</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/k8s/" rel="tag">k8s</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/k8s10-%E4%B8%AA%E5%B8%B8%E8%A7%81%E6%8C%91%E6%88%98%E5%92%8C%E9%99%B7%E9%98%B1/" rel="tag">k8s10 个常见挑战和陷阱</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/" rel="tag">nio</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BB%80%E4%B9%88%E6%98%AFeBPF/" rel="tag">什么是eBPF</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BB%8Edocker%E5%88%B0k8s/" rel="tag">从docker到k8s</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" rel="tag">分布式</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8%E5%8F%8A%E4%BA%8B%E5%8A%A1%E4%B8%80%E8%87%B4%E6%80%A7%E8%AE%BE%E8%AE%A1/" rel="tag">分库分表及事务一致性设计</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%AF%E8%A7%82%E6%B5%8B%E9%A2%86%E5%9F%9F%E5%BA%95%E5%B1%82%E6%8A%80%E6%9C%AF/" rel="tag">可观测领域底层技术</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%90%8E%E7%AB%AF/" rel="tag">后端</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%A6%82%E4%BD%95%E5%9F%BA%E4%BA%8E-k8s%E5%81%9A%E7%A7%81%E6%9C%89%E5%8C%96%E9%83%A8%E7%BD%B2/" rel="tag">如何基于 k8s做私有化部署</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B6%E5%8F%91/" rel="tag">并发</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9E%B6%E6%9E%84%E6%88%90%E9%95%BF%E6%8C%87%E5%8D%97/" rel="tag">架构成长指南</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%A7%81%E6%9C%89%E5%8C%96%E4%BA%A4%E4%BB%98/" rel="tag">私有化交付</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%A7%81%E6%9C%89%E5%8C%96%E9%83%A8%E7%BD%B2/" rel="tag">私有化部署</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B3%BB%E7%BB%9F%E9%83%A8%E7%BD%B2%E6%9E%B6%E6%9E%84%E6%BC%94%E8%BF%9B/" rel="tag">系统部署架构演进</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%93%BE%E8%A1%A8/" rel="tag">链表</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-link"></i> 友情链接</h3>
    <div class="widget">
      <ul>
      
        <li>
          <a target="_blank" rel="noopener" href="https://juejin.cn/user/1292681402913080">我的掘金</a>
        </li>
      
        <li>
          <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/dong-wei-zhao-73">我的知乎</a>
        </li>
      
        <li>
          <a target="_blank" rel="noopener" href="https://blog.csdn.net/dweizhao?type=blog">我的CSDN</a>
        </li>
      
      </ul>
    </div>
  </div>


  
</aside>
        
      </div>
      <a id="totop" href="#top"></a>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      <p>
        <a href="/sitemap.xml">网站地图</a>
        <span> | </span><a href="/atom.xml">订阅本站</a>
        <span> | </span><a href="/about/">联系博主</a>
      </p>
      
        <p>
          <i class="fa fa-visitors"></i>
          <i id="busuanzi_container_site_uv"><i id="busuanzi_value_site_uv"></i></i>
          ，
          <i class="fa fa-views"></i>
          <i id="busuanzi_container_site_pv"><i id="busuanzi_value_site_pv"></i></i>
        </p>
      
      <p>
        <span>Copyright &copy; 2023 架构成长指南.</span>
        <span>Theme by <a href="https://github.com/chaooo/hexo-theme-BlueLake/" target="_blank">BlueLake.</a></span>
        <span>Powered by <a href="https://hexo.io/" target="_blank">Hexo.</a></span>
      </p>
    </div>
  </div>
</footer>

    </div>
  </div>
  
<script src="/js/jquery-3.4.1.min.js"></script>


<script src="/js/search.json.js"></script>


  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>






  
<script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  
    
<script src="/localshare/js/social-share.js"></script>

    
<script src="/localshare/js/qrcode.js"></script>

  
  



  

  

  

  

  

  

  

  
  





</body>
</html>